/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.GUI;

import com.Algorithm.Variant;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 *
 * @author Jadzia
 */
public class ExcelReport {
    
    String PATH = "src/main/resources/report/RankingPracownikow.xls";
    
    public ExcelReport(List<Variant> rankingPromethee, List<Variant> rankingAHP) throws IOException
    {
        createReport(rankingPromethee, rankingAHP);
    }
    
    public void createReport(List<Variant> rankingPromethee, List<Variant> rankingAHP) throws FileNotFoundException, IOException {
        
        FileOutputStream fileOut = new FileOutputStream(PATH);
	HSSFWorkbook workbook = new HSSFWorkbook();
	       
        HSSFSheet prometheeWorksheet = workbook.createSheet("Promethee");
        writeSheet(rankingPromethee, prometheeWorksheet, workbook);
        
        HSSFSheet AHPWorksheet = workbook.createSheet("AHP");
        writeSheet(rankingAHP, AHPWorksheet, workbook);
               
        workbook.write(fileOut);
        fileOut.close();
    }
    
    public void writeSheet(List<Variant> ranking, HSSFSheet sheet, HSSFWorkbook workbook){
        
        createHeaderRow(sheet, createHeaderStyle(workbook));
        
        Row row = null;
        for(int i=0; i<ranking.size(); i++)
        {
            row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(i+1);
            row.createCell(1).setCellValue(ranking.get(i).getEmployee().getName());
            row.createCell(2).setCellValue(ranking.get(i).getEmployee().getSurname());
            row.createCell(3).setCellValue(ranking.get(i).getValue());
        }       
        sheet.setAutoFilter(new CellRangeAddress(0,ranking.size()-1,0,3));
        
        for (int i=0; i<4; i++){
            sheet.autoSizeColumn(i);
        }
    }
    
    public void createHeaderRow(HSSFSheet sheet, HSSFCellStyle style)
    {
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("Lp.");
        headerRow.createCell(1).setCellValue("Imię");
        headerRow.createCell(2).setCellValue("Nazwisko");
        headerRow.createCell(3).setCellValue("Wynik");
        
        for(int i = 0; i<4; i++){
            headerRow.getCell(i).setCellStyle(style);
        }
    }
    
    public HSSFCellStyle createHeaderStyle(HSSFWorkbook workbook){
        
        HSSFCellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(HSSFColor.YELLOW.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setAlignment(CellStyle.ALIGN_CENTER);

        HSSFFont font = workbook.createFont();
        
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        font.setColor(HSSFColor.BLACK.index);
        style.setFont(font);
        
        return style;
    }
}